#include "playpage.h"
#include "ui_playpage.h"
#include"role.h"
#include<QPainter>
#include<QPixmap>
#include<QIcon>
#include<QTimer>
#include"obstacle.h"
#include<QVector>
#include<vector>
#include<QDebug>
int playpage::x1=0;
int playpage::x2=1000;
int playpage::y=550;
int playpage::index=0;
playpage::playpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::playpage)
{
    ui->setupUi(this);
    setFixedSize(1000,700);
    setWindowTitle("Stick Runner");
    setWindowIcon(QIcon(":/image/Image/title.png"));
    timer->start(80);
    over=false;
    difficulty=0;
    groundy=650;
    connect(timer,&QTimer::timeout,[=](){//让地面和背景随着时间移动
        if(x1==-1000) x1=1000;
        if(x2==-1000) x2=1000;
        x1-=20;
        x2-=20;
        if(y<550){
            y+=25;
        }
        update();
    });
    connect(timer,&QTimer::timeout,[=](){
        if(index==4) index=0;
       index++;
       if(difficulty<=70){
           difficulty+=0.002;
       }
       for(auto i=barrier.begin();i!=barrier.end();i++){
           if((*i)->done()){
               addObstacle();
               i=barrier.erase(i);
           }
           else if((*i)->isCollision(y+5)){
               gameover();
           }
           if(!barrier.empty()){
               (*i)->move();
           }
       }
    });
    addObstacle();
    update();
}
void playpage::paintEvent(QPaintEvent *){
    QPainter painter(this);
    bg1.load(":/background/Image/bg.jpg");
    bg2.load(":/background/Image/bg.jpg");
    gd1.load(":/background/Image/ground.png");
    gd2.load(":/background/Image/ground.png");
    painter.drawPixmap(x1,0,1000,700,bg1);//跑步时的背景
    painter.drawPixmap(x2,0,1000,700,bg2);
    painter.drawPixmap(QRect(x1,650,1000,50),gd1);//画地面
    painter.drawPixmap(QRect(x2,650,1000,50),gd2);
    QRect q1(0,0,0,0);
    QRect q2(40,y,80,100);
    if(y==550){
    painter.drawPixmap(q2,hero.run[index],q1);
    }
    if(y<550){
    painter.drawPixmap(q2,hero.jump,q1);
    }
    for(auto i=barrier.begin();i!=barrier.end();i++)
    {
        painter.drawPixmap(QRect((*i)->getX(),(*i)->getY(),(*i)->getWidth(),(*i)->getHeight()),(*i)->getImg());
    }
    if(over){
        painter.drawPixmap(0,0,1000,700,QPixmap(":/image/Image/gameover.jpg"));
    }
    update();
}
void playpage::keyPressEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_K:{
        if(y==550) y-=300;
        if(250<=y&&y<550) y-=200;
        break;
    }
    }
}
void playpage::gameover(){
    over=true;
}
void playpage::addObstacle(){
        static int LastWall_time;
        srand(time(NULL));
        if(LastWall_time>=200-difficulty)
        {

            int x=this->width()+10;

            int y1=this->groundy-100-(rand()%(int)(650*0.3));
            barrier.push_back(new Wall(x,y1,70,groundy-y1));
            LastWall_time=0;
        }
        LastWall_time++;
}
playpage::~playpage()
{
    delete ui;
}
